package com.yanqu.road.server.cross3server.cmd.xrank;

import com.yanqu.road.logic.pb.XRankPb;
import com.yanqu.road.logic.xlogic.rank.user.XUserRankListModel;
import com.yanqu.road.logic.xlogic.rank.user.XUserRankMap;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.cross3server.manager.Cross3XRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;

@Cmd(code = Cross3Protocol.C3_XRANK_GET_USER_RANK_LIST, desc = "个人榜单")
public class XRankGetUserRankListFromGameCmd extends CrossCommand {

    private int activityId;
    private int conditionType;

    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XRankProto.GetXUserRankListReqMsg reqMsg = XRankProto.GetXUserRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getConditionType();

        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        XUserRankMap userRankMap = Cross3XRankMgr.getUserRankMap();
        XUserRankListModel rankListModel = userRankMap.getUserRankListModel(activityId, conditionType);

        XRankProto.XUserRankListMsg.Builder rankListMsg = XRankPb.parseXUserRankListMsg(activityId, conditionType, rankListModel.getRankList(), false);
        int myRank = rankListModel.getMyRank(userId);

        XRankProto.GetXUserRankListRespMsg.Builder builder = XRankProto.GetXUserRankListRespMsg.newBuilder();
        builder.setRet(0);
        XRankPb.fillFlagId(rankListMsg);
        builder.setRankList(rankListMsg);
        builder.setMyRank(myRank);
        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_XRANK_GET_USER_RANK_LIST, builder);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
